/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac4.repara;

import edu.uoc.tdp.pac4.beans.Mecanic;
import edu.uoc.tdp.pac4.beans.Peca;
import edu.uoc.tdp.pac4.beans.Reparacio;
import edu.uoc.tdp.pac4.beans.ReparacioMecanic;
import edu.uoc.tdp.pac4.cliente.Principal;
import edu.uoc.tdp.pac4.comun.GestorReparacionesInterface;
import edu.uoc.tdp.pac4.comun.PAC4Exception;
import edu.uoc.tdp.pac4.i18n.TDSLanguageUtils;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author rfebrerg
 */
public class GestionComandesCapTaller extends javax.swing.JInternalFrame {

    private GestorReparacionesInterface conexion;
    private int OrdreReparacio;
    private ArrayList<Peca> ComandaPeces;
    private final String URL = "localhost";
    private final String JNDI_NAME = "Serv_Reparaciones";
    private final int PORT = 1099;
    
    /**
     * Creates new form GestionReparaciones
     */
    public GestionComandesCapTaller(int idRep) throws PAC4Exception {
        OrdreReparacio=idRep;
        initComponents();
        conectarServerRMI();
        getInitialData();
    }

    private void conectarServerRMI() {
        try {
            Registry registry = LocateRegistry.getRegistry(URL, PORT);
            this.conexion = (GestorReparacionesInterface) registry.lookup(JNDI_NAME);
        } catch (NotBoundException e) {
            e.printStackTrace();
            System.exit(0);
        } catch (RemoteException e) {
            Principal.showError(e.getMessage());
            e.printStackTrace();
            System.exit(0);
        } catch (Exception e) {
            Principal.showError(e.getMessage());
            e.printStackTrace();
            System.exit(0);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        comandesTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, TDSLanguageUtils.getMessage("Reparaciones.TtlReparaciones"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.darkGray));

        comandesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Id", "Estado", "Mecánico", "Asignada", "Contador", "Observaciones", "NumCom", "Fecha Inicio", "Fecha fin"
            }
        ));
        jScrollPane2.setViewportView(comandesTable);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 576, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GestionComandesCapTaller.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GestionComandesCapTaller.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GestionComandesCapTaller.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GestionComandesCapTaller.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable comandesTable;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    private void getInitialData() throws PAC4Exception {
        getAllComandes();
      
    }
 private int getSelectedTable()
    {
         int row=comandesTable.getSelectedRow();
        int idRep=Integer.parseInt(comandesTable.getValueAt(row, 0).toString());
        return idRep;
    }
    private void getAllComandes() throws PAC4Exception {
        try {

            DefaultTableModel dtm = new DefaultTableModel(0, 0);
            String header[] = new String[]{
                TDSLanguageUtils.getMessage("Reparaciones.Id"),
                TDSLanguageUtils.getMessage("Reparaciones.Descripcion"), 
                TDSLanguageUtils.getMessage("Reparaciones.PVP"), 
                TDSLanguageUtils.getMessage("Reparaciones.Marca"), 
                TDSLanguageUtils.getMessage("Reparaciones.Modelo")
            };
            dtm.setColumnIdentifiers(header);
            comandesTable.setModel(dtm);
            Reparacio rep= new Reparacio();
            rep.setOrdreReparacio(OrdreReparacio);
            ComandaPeces= conexion.GetComandaPeces(rep);
                for (int i = 0; i < ComandaPeces.size(); i++) {
                Vector<Object> vdata = new Vector<Object>();
                vdata.add(ComandaPeces.get(i).getCodiPeca());
                vdata.add(ComandaPeces.get(i).getDescripcio());
                vdata.add(ComandaPeces.get(i).getPvp());
                vdata.add(ComandaPeces.get(i).getMarca());
                vdata.add(ComandaPeces.get(i).getModel());
                             
                dtm.addRow(vdata);
            }
            TableRowSorter sorter = new TableRowSorter(	dtm);
            comandesTable.setRowSorter(sorter);
            if(ComandaPeces.size()>0)
            comandesTable.setRowSelectionInterval(0, 0);
        } catch (RemoteException ex) {
            Principal.showError(ex.getMessage());
            Logger.getLogger(GestionComandesCapTaller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Principal.showError(ex.getMessage());
            Logger.getLogger(GestionComandesCapTaller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

   
}
